﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AIH = Functionality.AuditInformationHelper;
using CPDPWEM.Utils;

namespace Functionality
{
    public class DoorPrizeEvent
    {
        public DoorPrizeEvent(int seed, int prizeLimitCount) : this()
        {
            Random = new Random(seed);
            PrizeLimitCount = prizeLimitCount;
        }

        public DoorPrizeEvent(int prizeLimitCount) : this()
        {
            PrizeLimitCount = prizeLimitCount;
        }

        private DoorPrizeEvent()
        {
            Participants = new List<Person>();
            Prizes = new List<Prize>();
            PrizeDrawings = new List<PrizeDrawing>();
            Random = new Random();
            NextPrizeNumber = 0;
        }

        public TimeSpan Duration { get; set; }
        public int NumberOfDrawingsPerTick { get; set; }
        public string Name { get; set; }
        public List<Person> Participants { get; private set; }
        public List<Prize> Prizes { get; private set; }
        public List<PrizeDrawing> PrizeDrawings { get; private set; }

        public int RemainingNumberOfPrizes
        {
            get { return Prizes.Count - NextPrizeNumber; }
        }

        public int PrizeLimitCount { get; protected set; }
        protected Random Random { get; private set; }
        protected int NextPrizeNumber { get; set; }

        public void StartAutomaticDrawings(bool startWithATick)
        {
            // Game is afoot! Shuffle the prizes to start (people are shuffled each time a drawing occurs).
            Prizes.Shuffle();
        }

        public void DrawingTick()
        {
        }

        public void DrawPrize()
        {
            if (RemainingNumberOfPrizes <= 0)
                throw new Exception("No more prizes to select from!!!");

            Person recipient = SelectPerson();
            Prize prize = SelectNextPrize();

            recipient.PrizesWon.Add(prize);
            PrizeDrawings.Add(new PrizeDrawing(recipient, prize));

            AIH.Log("{0} Prize Drawn\t{1}\t{2}", Name, recipient.FullName, prize.Name);
        }

        public void EndEvent()
        {
            AIH.Log("{0} Event Complete", Name);
        }

        protected Person SelectPerson()
        {
            // Limit who we select from by the number of prizes won.
            List<Person> toSelectFrom = (from person in Participants
                                        where person.PrizesWon.Count < PrizeLimitCount
                                        select person).ToList();

            // Need to have at least one person to draw for.
            if (!toSelectFrom.Any())
                throw new Exception("No valid options to select a person from");

            toSelectFrom.Shuffle();
            return toSelectFrom[0];
        }

        protected Prize SelectNextPrize()
        {
            if (NextPrizeNumber >= Prizes.Count)
                throw new Exception("Out of prizes to select from");
            return Prizes[NextPrizeNumber++];
        }
    }
}
